define([
    'angular',
    'angularAMD',
    'core/controllers/dialog',
    'css!css/core/dialog',
    'angular-ngDialog'
], function(angular, angularAMD, dialogs) {
    var factory = angular.module('app.factory.dialog', ['ngDialog']);
    
    // ngDialg
    factory.config(['$provide', 'ngDialogProvider', function($provide, ngDialogProvider) {
        ngDialogProvider.setDefaults({
            bodyClassName: '',// ngdialog-open
            className: 'ngdialog-theme-default',
            plain: false,
            showClose: true,
            closeByDocument: false,
            closeByEscape: true,
            appendTo: false,
            disableAnimation: true,
            preCloseCallback: function () {
                //console.log('Default pre-close callback');
            }
        });

        $provide.factory('Dialog', ['$q', 'ngDialog', function($q, ngDialog) {
            var dialogFactory = {};

            dialogFactory.$alert = function(message) {
                var defer = $q.defer();
                ngDialog.open({
                    template: [
                        '<div class="ngdialog-message">',
                        '  <h3>提示信息</h3>',
                        '  <div class="message" ng-bind="message"></div>',
                        '</div>',
                        '<div class="ngdialog-buttons">',
                        '  <button type="button" class="ngdialog-button ngdialog-button-primary" ng-click="closeThisDialog()">确 定</button>',
                        '</div>'
                    ].join(''),
                    plain: true,
                    controller: ['$scope', function($scope) {
                        $scope.message = message;
                    }]
                }).closePromise.then(function(data) {
                    defer.resolve(data);
                });

                return defer.promise;
            };

            dialogFactory.$confirm = function(message) {
                var defer = $q.defer();
                ngDialog.openConfirm({
                    template: [
                        '<div class="ngdialog-message">',
                        '  <h3>确认信息</h3>',
                        '  <div class="message" ng-bind="message"></div>',
                        '</div>',
                        '<div class="ngdialog-buttons">',
                        '  <button type="button" class="ngdialog-button ngdialog-button-secondary" ng-click="closeThisDialog()">取 消</button>',
                        '  <button type="button" class="ngdialog-button ngdialog-button-primary" ng-click="confirm()">确 定</button>',
                        '</div>'
                    ].join(''),
                    plain: true,
                    controller: ['$scope', function($scope) {
                        $scope.message = message;
                    }]
                })
                .then(function(data) {
                    defer.resolve(data);
                }, function(data) {
                    defer.reject(data);
                });

                return defer.promise;
            };

            dialogFactory.$dialog = function(options) {
                var defer = $q.defer();
                ngDialog.open(options).closePromise.then(function(data) {
                    defer.resolve(data);
                });

                return defer.promise;
            };

            // load dialogs
            var modalDialogs = {};
            angular.forEach(dialogs, function(dialogConfigs) {
                angular.forEach(dialogConfigs, function(dialogConfig) {
                    modalDialogs[dialogConfig.method] = function(context) {
                        var _context = angular.extend({}, context);
                        var defer = $q.defer();
                        var _dialogConfig = angular.extend({
                            controllerAs: 'dvm',
                            resolve: {
                                Context: function() { return _context; }
                            }
                        }, dialogConfig);

                        var $dialog = ngDialog.open(angularAMD.route(_dialogConfig));
                        $dialog.closePromise.then(function() {
                            defer.resolve(_context);
                        });

                        angular.extend(_context, {
                            close: function() {
                                $dialog.close();
                            },
                            closeAll: function() {
                                ngDialog.closeAll();
                            }
                        });

                        return defer.promise;
                    };
                });
            });
            dialogFactory.$open = function() {
                return modalDialogs;
            };

            return dialogFactory;
        }]);
    }]);
    
    return factory;
});